# pulls GNN data from the fsl.orst.edu server 
import sys, os
from itertools import product
from urllib import urlretrieve as retrieve

def download (states='ca', dProd= 'GNN structure model', filtered='filtered', plotDB=True):
    ''' to add multiple parameters variable should be in list form ['ca','or']. 
    states can be 'ca', 'or', or 'wa'
    dProd can be 'GNN structure model', GNN species-size map', 'GNN structure model' 
    here is no filtered product for species map and no unfiltered for species-size map '''
    urlBase='http://www.fsl.orst.edu/lemma/export/grids/gnnfire/%s_%s_%s.zip'
    PurlBase='http://www.fsl.orst.edu/lemma/export/grids/gnnfire/%s_%s.zip'
    filt={'unfiltered':'unf','filtered':'fil'}
    dataProduct={'GNN species map':'spp', 'GNN species-size map':'sppsz', 'GNN structure model':'struct'}
    st=['ca','or','wa']
    sStates= [i for i in st if i in states]
    sFilt=[filt[f] for f in filt if f in filtered]
    sProd = [dataProduct[p] for p in dataProduct if p in dProd]
    if plotDB:
        pLts=[PurlBase%p for p in product(sStates, ['plots'])]
    else:
        pLts=[]
    print {'grids':[urlBase%i for i in product(sStates, sProd, sFilt)], 'plotdb':pLts}


# Then open the new URL which is the same as clicking the submit button
#grds=download(states=['%s'%sys.argv[1]])['grids']
#plotD=download(states=['%s'%sys.argv[1]])['plots']
#[retrieve(f, os.path.split(f)[1]) for f in grds]
#[retrieve(f, os.path.split(f)[1]) for f in plotD]